##########################################
########## Tone Analyses ###############

# Data In: "monthly_tone.csv"
         # "post_tone.csv"
          
# Data Out: 
# Figures 7a, 7b, 8, 9

###########################################

#Load Packages
library(readr)
library(tidyverse)
library(lubridate)


# Set options for plotting 
options(scipen=999999)

# Set working directory to replication folder (Refugee_Info_Replication)

# Open code file from "Refugee_Info_Replication/code/post_type_analysis.R"

# Obtain the full path of the current script in RStudio
script_path <- rstudioapi::getActiveDocumentContext()$path

# If the script path is non-empty, proceed to set the working directory
if (!is.null(script_path)) {
  # Calculate the parent directory of the script's directory
  parent_directory <- dirname(dirname(script_path))
  
  # Set the working directory to the parent directory
  setwd(parent_directory)
} else {
  cat("Script path is not set. Ensure your script is saved and you are running RStudio.")
}
# Check Working Directory
getwd()

# Read in Monthly Tone Data 
monthly_tone<-read_csv("data/monthly_tone.csv")

# Read in Document-Level Tone Data

post_tone<-read_csv("data/post_tone.csv")

#############
# FIGURE 7a #
#############

# Aggregate Cosine Similarity 

cos_agg<-na.omit(post_tone)%>%
  group_by(post_type)%>%
  summarise(mean = mean(diff_ar))

cos_agg2<-post_tone%>%
  group_by(post_type)%>%
  summarise(mean = mean(na.omit(diff_ar)))

ggplot(cos_agg, aes(fill=post_type, y=mean, x=post_type)) + 
  geom_bar(position="dodge", stat="identity", color="black")+theme_minimal(base_size=22)+
  labs(y = "Cosine Similarity \n (Encouraging-Discouraging)", x = "Source")+
  theme(axis.text.x=element_text(angle=90,hjust=1))+
  scale_fill_grey()+
  theme(legend.title=element_blank())
ggsave("plots/Figure_7a.pdf", width = 11, height = 7)


#############
# FIGURE 7b #
#############

official_unofficial<-subset(monthly_tone, source!="news")

official_unofficial %>% 
  ggplot() + 
  aes(x = seqvar, y = diff_ar_combined, group=source, color=source, fill=source) + 
  geom_smooth(method=loess, span=.2, se=FALSE) +
  geom_point()+
  labs(y = "Monthly Cosine Similarity \n (Encouraging-Discouraging)", x = "Date")+
  theme_minimal(base_size=22)+
  scale_color_grey()
ggsave("plots/Figure_7b.pdf", width = 11, height = 7)

#############
# FIGURE 8 #
#############

# Identify top quartile of encouraging and discouraging posts 
summary(post_tone$diff_ar)
encouraging<-subset(post_tone, diff_ar>=.0974)
discouraging<-subset(post_tone, diff_ar<=-.0276)

# Calculate Mean Engagement for Encouraging vs. Discouraging posts 
aggregate_encouraging= encouraging %>% 
  group_by(post_type) %>%
  summarise(mean=mean(engagement_fb))

# Same for discouraging
aggregate_discouraging= discouraging %>% 
  group_by(post_type) %>%
  summarise(mean = mean(engagement_fb))

# Label 
aggregate_encouraging$sentiment<-"encouraging"
aggregate_discouraging$sentiment<-'discouraging'
ag_total<-rbind(aggregate_encouraging, aggregate_discouraging)

# Plot 
ggplot(ag_total, aes(fill=sentiment, y=mean, x=post_type)) + 
  geom_bar(position="dodge", stat="identity", color="black")+theme_minimal(base_size=22)+
  labs(y = "Mean Engagement per Post \n", x = "Source")+
  scale_fill_grey()+
  theme(legend.title=element_blank())
ggsave("plots/Figure_8.pdf", width = 11, height = 7)


#############
# FIGURE 9 #
#############

cos_agg<-na.omit(post_tone)%>%
  group_by(pre_2016_source)%>%
  summarise(mean = mean(diff_ar))

ggplot(cos_agg, aes(fill=pre_2016_source, y=mean, x=pre_2016_source)) + 
  geom_bar(position="dodge", stat="identity", color="black")+theme_minimal(base_size=22)+
  labs(y = "Cosine Similarity \n (Encouraging-Discouraging)", x = "Source")+
  scale_fill_grey()+
  theme(legend.title=element_blank())
ggsave("plots/Figure_9.pdf", width = 11, height = 7)




